<!--
 * @Author: zqx me_zqx@163.com
 * @Date: 2025-02-20 15:47:15
 * @LastEditors: zqx me_zqx@163.com
 * @LastEditTime: 2025-09-01 18:22:00
 * @FilePath: /XMate-h5/src/views/myXmate/myTeam/teamNoticeBoard/visitSituation/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="score-card p-4 overflow-auto">
    <!-- 沟通计划卡片 -->
    <ScoreCardItem
      title="沟通计划"
      type="visitPlan"
      tooltip-content=""
      :overviewData="teamOverview"
      :isTeam="isTeam"
      :memberData="memberBoardList"
      :teamData="teamBoardList"
      :items="visitPlanOverview"
    />

    <ScoreCardItem
      title="沟通记录"
      type="visitRecord"
      tooltip-content="沟通总次数：仅统计有效沟通次数，沟通记录处理状态为【处理失败、文件处理中、会话字幕量不足】标识为无效数据"
      :overviewData="teamOverview"
      :isTeam="isTeam"
      :memberData="memberBoardList"
      :teamData="teamBoardList"
      :items="visitRecordOverview"
    />

    <!-- <ScoreCardItem v-ai-tip="'bottom-right'" title="目标达成" type="targetAchievement"
      tooltip-content=" 沟通目标评估结果为「达成」的沟通次数 / 沟通总次数" :overviewData="teamOverview" :isTeam="isTeam" :enableChart="!isTeam"
      :memberData="memberBoardList" :teamData="teamBoardList" :items="[
        {
          title: '沟通目标达成率',
          content: formatValue(teamOverview.visitTargetAchievementRate, 2, true),
          unit: '%',
          props: 'visitTargetAchievementRate',
        },
      ]" /> -->

    <ScoreCardItem
      title="沟通表现"
      type="visitPerformance"
      tooltip-content="1. 能力评估达标率：沟通辅导中能力评估得分 >= 80分的沟通次数 / 沟通总次数
      2. 任务完成达标率：沟通辅导中任务达成率 >= 80%的沟通次数 / 沟通总次数"
      :overviewData="teamOverview"
      :isTeam="isTeam"
      :enableChart="!isTeam"
      :memberData="memberBoardList"
      :teamData="teamBoardList"
      :items="[
        {
          title: '能力评估达标率',
          content: formatValue(teamOverview.salesAbilityPassRate, 2, true),
          unit: '%',
          props: 'salesAbilityPassRate',
        },
        {
          title: '任务完成达标率',
          content: formatValue(teamOverview.taskCompletePassRate, 2, true),
          unit: '%',
          props: 'taskCompletePassRate',
        },
      ]"
    />

    <div id="score-table" class="rounded-lg bg-white overflow-clip shadow-sm pb-5">
      <ScoreTable
        v-ai-tip="'bottom-right'"
        :memberBoardList="memberBoardList"
        :teamBoardList="teamBoardList"
        :selectedMemberList="selectedMemberList"
        :isTeam="isTeam"
        @onFilterPeople="handleFilterPeople"
      />
    </div>
  </div>
</template>

<script setup>
import ScoreCardItem from "../../../components/scoreCardItem.vue";
import ScoreTable from "../../../components/scoreTable.vue";
import { onMounted, computed, ref, provide, watch } from "vue";
import { formatValue } from "@/utils/num";
import { useCommonStore, useMyXmateStore } from "@/store";

const myXmateStore = useMyXmateStore();
const commonStore = useCommonStore();

onMounted(() => {
  getDate();
});

const mydprt = computed(() => myXmateStore.selfDprt);
const memberBoardList = computed(() => myXmateStore.memberBoardList);
const teamBoardList = computed(() => myXmateStore.teamBoardList);

const currentDeptId = computed(() => myXmateStore.noticeBoardPublicParam.currentDept?.id);
const reportType = computed(() => myXmateStore.noticeBoardPublicParam.reportType);
const startTime = computed(() => myXmateStore.noticeBoardPublicParam.startDate);
const endTime = computed(() => myXmateStore.noticeBoardPublicParam.endDate);
const isLastLevel = computed(() => myXmateStore.isLastLevelDepartment);
const orgId = computed(() => myXmateStore.noticeBoardPublicParam.orgId);
const isTeam = computed(() => {
  return myXmateStore.noticeBoardPublicParam.currentDept?.children?.length > 0;
});

// 是否存在商机
const enableBusinessOpportunity = computed(() => commonStore.enableBusinessOpportunity);

// 团队看板总览
const teamOverview = computed(() => myXmateStore.teamOverview);
// 沟通计划总览表
const visitPlanOverview = computed(() => {
  const arr = [];

  arr.push({
    title: "沟通计划总数",
    content: teamOverview.value.totalVisitPlanCount,
    props: "totalVisitPlanCount",
  });

  // arr.push({
  //   title: "沟通计划完成率",
  //   content: formatValue(teamOverview.value.visitPlanCompletionRate, 2, true),
  //   props: "visitPlanCompletionRate",
  //   unit: "%",
  // });

  arr.push({
    title: "沟通计划覆盖客户数",
    content: teamOverview.value.visitPlanCustomerCount,
    props: "visitPlanCustomerCount",
  });

  if (enableBusinessOpportunity.value) {
    arr.push({
      title: "沟通计划覆盖商机数",
      content: teamOverview.value.visitPlanOpportunityCount,
      props: "visitPlanOpportunityCount",
    });
  }

  arr.push({
    title: "人均沟通计划数",
    content: teamOverview.value.averageVisitPlanPerPerson?.toFixed(2),
    props: "averageVisitPlanPerPerson",
    fixedNum: 2,
  });

  return arr;
});

const visitRecordOverview = computed(() => {
  let arr = [];
  arr.push({
    title: "沟通总次数",
    content: teamOverview.value.totalVisitCount,
    props: "totalVisitCount",
  });
  arr.push({
    title: "沟通覆盖客户数",
    content: teamOverview.value.visitedCustomerCount,
    props: "visitedCustomerCount",
  });

  if (enableBusinessOpportunity.value) {
    arr.push({
      title: "沟通覆盖商机数",
      content: teamOverview.value.visitedOpportunityCount,
      props: "visitedOpportunityCount",
    });
  }

  arr.push({
    title: "人均沟通次数",
    content: formatValue(teamOverview.value.averageVisitsPerPerson, 2),
    props: "averageVisitsPerPerson",
    fixedNum: 2,
  });
  return arr;
});

provide("teamNoticeBoard/teamId", currentDeptId);
provide("teamNoticeBoard/orgId", orgId.value);
provide("teamNoticeBoard/periodType", reportType.value);

async function getDate() {
  // myXmateStore.getSystemTagsAction();
  await myXmateStore.getMydprtAction();
  getListData();
}

const page = ref(1);
const pageSize = ref(300);

const params = {
  pageSize: pageSize.value,
  pageNumber: page.value,
  orderBy: "",
  asc: true,
};

watch([currentDeptId, reportType], () => {
  getListData();
});

async function getListData() {
  // ssoUserIds
  myXmateStore.getMemberBoardAction(currentDeptId.value, reportType.value, params);
  myXmateStore.getTeamBoardAction(currentDeptId.value, reportType.value, params);
  myXmateStore.getTeamBoardOverviewAction(currentDeptId.value, reportType.value);
  myXmateStore.getTeamBoardUserAction(currentDeptId.value, reportType.value);
  myXmateStore.getTeamBoardSubteamAction(currentDeptId.value, reportType.value);
}

const selectedMemberList = ref([]);
// 筛选成员
function handleFilterPeople(memberList) {
  selectedMemberList.value = memberList;
  const ssoUserIds = memberList.map((item) => item.id);
  const filterParams = {
    ssoUserIds: ssoUserIds,
    ...params,
  };
  // 获取团队成员沟通情况统计报告-分页
  myXmateStore.getMemberBoardAction(currentDeptId.value, reportType.value, filterParams);
}
</script>
